package com.ruoyi.project.digital.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.digital.domain.Pet;
import com.ruoyi.project.digital.domain.enums.YesOrNoStatus;
import com.ruoyi.project.digital.domain.vo.UserPetVo;
import com.ruoyi.project.digital.service.IPetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.digital.mapper.UserPetMapper;
import com.ruoyi.project.digital.domain.UserPet;
import com.ruoyi.project.digital.service.IUserPetService;

/**
 * 用户与宠物关系Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-30
 */
@Service
public class UserPetServiceImpl extends ServiceImpl<UserPetMapper, UserPet> implements IUserPetService {
    @Autowired
    private IPetService petService;

    /**
     * 查询用户与宠物关系
     *
     * @param id 用户与宠物关系ID
     * @return 用户与宠物关系
     */
    @Override
    public UserPet selectUserPetById(Long id) {
        return baseMapper.selectUserPetById(id);
    }

    /**
     * 查询用户与宠物关系列表
     *
     * @param userPet 用户与宠物关系
     * @return 用户与宠物关系
     */
    @Override
    public List<UserPet> selectUserPetList(UserPet userPet) {
        return baseMapper.selectUserPetList(userPet);
    }

    /**
     * 新增用户与宠物关系
     *
     * @param userPet 用户与宠物关系
     * @return 结果
     */
    @Override
    public int insertUserPet(UserPet userPet) {
        userPet.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertUserPet(userPet);
    }

    /**
     * 修改用户与宠物关系
     *
     * @param userPet 用户与宠物关系
     * @return 结果
     */
    @Override
    public int updateUserPet(UserPet userPet) {
        userPet.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateUserPet(userPet);
    }

    /**
     * 批量删除用户与宠物关系
     *
     * @param ids 需要删除的用户与宠物关系ID
     * @return 结果
     */
    @Override
    public int deleteUserPetByIds(Long[] ids) {
        return baseMapper.deleteUserPetByIds(ids);
    }

    /**
     * 删除用户与宠物关系信息
     *
     * @param id 用户与宠物关系ID
     * @return 结果
     */
    @Override
    public int deleteUserPetById(Long id) {
        return baseMapper.deleteUserPetById(id);
    }

    @Override
    public void initMessage(String nickName, Long petId, Long userId) {
        QueryWrapper<UserPet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pet_id", petId);
        queryWrapper.eq("user_id", userId);
        UserPet userPet = getOne(queryWrapper);
        userPet.setPetName(nickName);
        updateById(userPet);
    }

    /**
     * 首页宠物
     *
     * @param id
     * @return
     */
    @Override
    public List<UserPetVo> frontList(Long id) {
        //查询全部宠物信息
        List<Pet> petList = petService.list();
        //查询用户拥有的宠物信息
        QueryWrapper<UserPet> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.eq("user_id", id);
        List<UserPetVo> userPetList = baseMapper.selectFrontList(queryWrapper);
        //整合下数据
        List<UserPetVo> result = handlerMessage(petList, userPetList, id);

        return result;
    }

    /**
     * 整合前端的数据
     *
     * @param petList     用户宠物
     * @param userPetList 用户拥有的宠物
     * @param id          用户ID
     * @return
     */
    private List<UserPetVo> handlerMessage(List<Pet> petList, List<UserPetVo> userPetList, Long id) {
        List<UserPetVo> result = new ArrayList<>();
        for (Pet pet : petList) {
            int  falg=0;
            for (UserPetVo vo : userPetList) {
                //如果存在，则判定下是否存在未读的数据
                if (vo.getPetId().equals(pet.getId())) {
                    Integer count = baseMapper.selectNoReadByUserId(id);
                    if (count != 0) {
                        vo.setIsRead(YesOrNoStatus.YES.getValue());
                    }else{
                        vo.setIsRead(YesOrNoStatus.NO.getValue());
                    }
                    vo.setXloc(pet.getXloc());
                    vo.setYloc(pet.getYloc());
                    vo.setPetUrl(pet.getPetUrl());
                    vo.setPetName(pet.getPetName());
                    vo.setFlag(YesOrNoStatus.YES.getValue().toString());
                    result.add(vo);
                    falg=1;
                }

            }
            //如果不存在
            if(falg==0){
                UserPetVo userPetVo=new UserPetVo();
                BeanUtils.copyProperties(pet,userPetVo);
                userPetVo.setUserId(id);
                userPetVo.setPetUrl(pet.getPetUrl());
                userPetVo.setXloc(pet.getXloc());
                userPetVo.setYloc(pet.getYloc());
                userPetVo.setPetName(pet.getPetName());
                userPetVo.setFlag(YesOrNoStatus.NO.getValue().toString());
                userPetVo.setPetId(pet.getId());
                userPetVo.setIsRead(YesOrNoStatus.NO.getValue());
                result.add(userPetVo);
            }
        }
        return  result;

    }
}
